package com.linewow.xhyy.nihaonew.activity.user;

import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.base.BaseActivity;
import com.linewow.xhyy.nihaonew.entity.NiHaoGeneral;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/10/28.
 */
public class AdviceActivity extends BaseActivity {
    @Bind(R.id.toolbar_back_title)
    TextView toolbarBackTitle;
    @Bind(R.id.toolbar_back_left)
    ImageView toolbarBackLeft;
    @Bind(R.id.toolbar_back_right_tv)
    TextView toolbarBackRightTv;
    @Bind(R.id.advice_content)
    EditText adviceContent;
    @Bind(R.id.advice_email)
    EditText adviceEmail;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_advice);
        ButterKnife.bind(this);
        toolbarBackRightTv.setVisibility(View.VISIBLE);
        toolbarBackTitle.setText(getString(R.string.title_advice));
        clickHide();
    }


    @OnClick({R.id.toolbar_back_left, R.id.toolbar_back_right_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back_left:
                finish();
                break;
            case R.id.toolbar_back_right_tv:
                String content=adviceContent.getText().toString();
                String contact=adviceEmail.getText().toString();
                requestInfo(content,contact);
                break;
        }
    }

    private void requestInfo(String content, String contact) {
        showLoading();
        NiHaoOkgo.adviceInfo(content,contact)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NiHaoGeneral<String>>() {
                    @Override
                    public void call(NiHaoGeneral<String> stringNiHaoGeneral) {
                        dismissLoading();
                        if(stringNiHaoGeneral.status){
                            Toast.makeText(AdviceActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }else {
                            Toast.makeText(AdviceActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.e(TAG, "错误信息" + throwable.getMessage());
                        showToast(getString(R.string.message_no_internet));
                    }
                });
    }
}
